package com.asd.map.controller;

import com.alibaba.fastjson.JSONArray;
import com.asd.common.Result;
import com.asd.map.entity.Map_Information_point;
import com.asd.map.entity.Map_apply;
import com.asd.map.entity.Tagging_style;
import com.asd.map.service.ApplyService;
import com.asd.map.service.ITagging_styleService;
import com.asd.system.entity.Role;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @createtime 2021-11-03 11:14
 * @description
 */

@RestController
@RequestMapping("/api/tagging_style")
@Api(tags = "标注样式Api")
public class Tagging_styleController {

    @Autowired
    private ITagging_styleService tagging_styleService;

    @Autowired
    private ApplyService applyService;


//    @ApiOperation(value = "标注样式-新增", notes = "标注样式-新增")
//    @PostMapping
//    public Result<?> save(@RequestBody Tagging_style tagging_style) {
//        return Result.ok(tagging_styleService.save(tagging_style));
//    }

    @ApiOperation(value = "标注样式-编辑", notes = "标注样式-编辑")
    @PutMapping
    public Result<?> update(@RequestBody Tagging_style tagging_style) {
        Result<Tagging_style> result = new Result<Tagging_style>();
        String tid = tagging_style.getTid();
        Tagging_style byTid = tagging_styleService.findBYTid(tid);
        if (byTid == null ){
            tagging_styleService.save(tagging_style);
            result.success("添加成功");
        }else {
            tagging_styleService.updateById(tagging_style);
            result.success("编辑成功");
        }
        return Result.ok(result);
    }


    @ApiOperation(value = "标注样式-根据应用id查询", notes = "标注样式-根据应用id查询")
    @GetMapping("selectAllByApplyId")
    public Result<?> selectAllByApplyId(@RequestParam("applyId") String applyId) {

//        Result<Tagging_style> result = new Result<Tagging_style>();


        List<Tagging_style> tagList = new ArrayList<>();

        Map_apply mapApply = applyService.getById(applyId);
        String data = mapApply.getData();
        //将字符串data转为list
        List<Map<String, Object>> listData = (List<Map<String, Object>>) JSONArray.parse(data);
        for (int i = 0; i < listData.size(); i++) {
            Map<String, Object> map = listData.get(i);
            String tid = map.get("tid").toString();

            Tagging_style taggingStyle = tagging_styleService.findBYTid(tid);
            if (taggingStyle == null) {
                continue;//跳过循环体中剩余的语句而执行下一次循环
            } else {
                tagList.add(taggingStyle);
//                result.ok(tagList);
            }

        }
        return Result.ok(tagList);
    }

}

